﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.BLL.Extend;
using ZX.DBModule;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace ZX.Web.UI.Controls
{


    public class EditDataGrid : Literal
    {
        public override string ID
        {
            get
            {
                return base.ID;
            }
            set
            {
                base.ID = value;
            }
        }

        public string EntityName
        {
            get;
            set;
        }

        public string MasterName
        {
            get;
            set;
        }

        public string FKey
        {
            get;
            set;
        }

        private string CreateDataTableJS()
        {
            string Html = @"
//----------------------------------------------
// 表单自定义的数据
// Version :V 0.1.0
// ----------------------------------------------------------
_DataTable{0} = {
    verion: ""v 0.1.0"",
    tableId: """",
    author: ""江怀玉"",
    Editable: false,
    Entity: """",
    ///列名集合
    Column: [],
    ///行值集合
    Rows: [],
    ///定义当前行
    CureentRow: """",
    ///获取行
    getRow: function (i) {
        return this.Rows[i];
    },
    ///获取当前实体
    getEntity: function () {
        return $($(""#Details table"")[0]).attr(""name"");
    },
    ///获取列名集合
    getColumn: function () {
        var _Column = [];
        $($(""#Details table"")[0]).find(""input"").each(function () {
            _Column.push($(this).attr(""name""));
            $(this).val("""");
        });
        this.Column = _Column;
    },
    ///获取当前行指定的列值
    getColumnValue: function (o) {
        if (typeof (o) == ""String"") {
            return eval(""this.CureentRow."" + o);
        }
        else {
            return eval(""this.CureentRow."" + Column[o]);
        }
    },
    ///数据
    _DataBind: function () {
 
        //alert(this.Entity);
        this.Entity = this.getEntity();
        this.getColumn();
        this.DataBind(this.Entity);
        this.getDataSource();
    },
    ///获取数据源
    getDataSource: function () {
  
        var strUrl = ""AjaxData/DataTableJsonlist.aspx?r="" + Math.random() + ""&pageindex=0&pagesize=500&entity="" + this.Entity;
        var col = this.Column.length;
        $.get(strUrl, function (data, textStatus) {
            _DataTable.Rows = eval(""("" + data + "")"").rows;
            _DataTable.Render();
            var trs = $(""#"" + _DataTable.Entity + "" tr"");
            for (var i = 0; i < _DataTable.Rows.length; i++) {
                $(trs[i + 1]).JSONToForm(_DataTable.Rows[i]);
            }
            var th = $(""<thead><thead>"").append($(""#"" + _DataTable.Entity + "" tr:first""));
            $(""#"" + _DataTable.Entity).prepend(
            th
            );
             $(""#"" + _DataTable.Entity + "" tr:first"").remove();
            $(""#"" + _DataTable.Entity+"" thead:first"").prepend(
                $(""<tr><td colspan=\"""" + col + ""\""><button id='_btnNew' onclick='_DataTable.AddRow();return false;'>新增</button></td></tr>"")
            );
            //$(""#"" + _DataTable.Entity + "" tr:first"").remove();
           
        });
    },
    ///重写控制
    Render: function () {
        //var trs = $($($(""#"" + this.Entity + "" tr"")[1]).html());
        if (this.Rows.length > 1) {
            for (var i = 1; i < this.Rows.length; i++) {
                var trs = $($(""#"" + this.Entity + "" tr"")[1].outerHTML.replace(""data_row"", ""id"" + i));
                trs.appendTo($($(""#Details table"")));
            }
        }
    },
    //增加行
    AddRow: function () {
        this.Rows.push(eval(""{0}""));
        var i = this.Rows.length - 1
        var trs = $($(""#"" + this.Entity + "" tr"")[2].outerHTML.replace(""data_row"", ""id"" + this.Rows.length - 1));
        trs.appendTo($($(""#Details table"")));
        $(trs[i + 1]).JSONToForm(_DataTable{0}.Rows[i]);
    },
    //删除行
    DeleteRow: function () {
    },
    //修改
    Modify: function (o) {
    }
}

if ($(""#Details"").length > 0) {
    _DataTable{0}._DataBind();
    //_DataTable{0}.AddRow();
}"";

";
            return "";
        }


        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
        }
  
    }
}

